﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows;

namespace AttentenceCheckSystem
{
    class HolidayGen
    {
        public void AddHoliday(int uid, DateTime date)
        {
            try
            {
                string strConn = Properties.Settings.Default.Database1ConnectionString;
                using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn))
                {
                    conn.Open();
                    OleDbCommand command = conn.CreateCommand();
                    command.CommandText = "SELECT * FROM 打卡记录 WHERE uid=@uid AND 日期=@date";
                    command.Parameters.Add("@uid", OleDbType.Integer).Value = uid;
                    command.Parameters.Add("@date", OleDbType.Date).Value = date.Date;
                    OleDbDataReader reader = command.ExecuteReader();
                    if (!reader.HasRows)
                        return;

                    if (!reader.Read())
                        return;
                    if (reader["记录2"] is DBNull)
                        return;
                    DateTime startTime = Convert.ToDateTime(reader["记录1"]);
                    DateTime endTime = Convert.ToDateTime(reader["记录2"]);
                    TimeSpan OverworkTime = new TimeSpan(endTime.Ticks - startTime.Ticks);
                    if (OverworkTime.TotalHours < 1)
                        return;

                    reader.Close();

                    command.CommandText = "SELECT * FROM holidays WHERE uid=@uid AND 类型=2 AND 月份=@month";
                    command.Parameters.Clear();
                    command.Parameters.Add("@uid", OleDbType.Integer).Value = uid;
                    command.Parameters.Add("@month", OleDbType.Date).Value = common.GetCheckStartDate(date);
                    reader = command.ExecuteReader();
                    if (reader.HasRows)
                    {
                        reader.Read();
                        reader.Close();
                        command.CommandText = string.Format("UPDATE holidays SET 时长=时长+{0} WHERE uid=@uid AND 类型=2 AND 月份=@month", Convert.ToInt32(OverworkTime.TotalHours));
                    }
                    else
                    {
                        reader.Close();
                        command.CommandText = string.Format("INSERT INTO holidays(uid, 月份, 类型, 时长) VALUES (@uid, @month, 2, {0})",
                            Convert.ToInt32(OverworkTime.TotalHours));
                    }
                    command.Parameters.Clear();
                    command.Parameters.Add("@uid", OleDbType.Integer).Value = uid;
                    command.Parameters.Add("@month", OleDbType.Date).Value = common.GetCheckStartDate(date);
                    command.ExecuteNonQuery();

                    command.CommandText = "UPDATE 加班申请 SET Calculated=1 WHERE uid=@uid AND 日期=@date";
                    command.Parameters.Clear();
                    command.Parameters.Add("@uid", OleDbType.Integer).Value = uid;
                    command.Parameters.Add("@date", OleDbType.Date).Value = date.Date;
                    int i = command.ExecuteNonQuery();
                }
            }
            catch (Exception ec)
            {
                MessageBox.Show(ec.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
